/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.importer.impl;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dafe.partyband.db.api.ISong;
import org.dafe.partyband.db.api.ISongResource;
import org.dafe.partyband.importer.api.ISongImport;

/**
 *
 * @author dafe
 */
public class SongImport implements ISongImport {

    private ISong dbSong;
    private ISong importSong;
    private IEvaluation evaluation;

    public ISong getSongInDb() {
        return dbSong;
    }

    public void setSongInDb(ISong song) {
        dbSong = song;
    }

    public ISong getSongToImport() {
        return importSong;
    }

    public void setSongToImport(ISong song) {
        importSong = song;
    }

    public IEvaluation getEvaluation() {
        return evaluation;
    }

    public void setEvaluation(IEvaluation eval) {
        this.evaluation = eval;
    }

    public static class Evaluation implements IEvaluation {

        private List<IImportIssue> issues = new ArrayList<IImportIssue>();
        private Map<Field, Boolean> importFields = new EnumMap<Field, Boolean>(Field.class);
        private Map<ISongResource, ResourceImport> importResources = new HashMap<ISongResource, ResourceImport>();

        public List<? extends IImportIssue> getIssues() {
            return issues;
        }

        public void addIssue(IImportIssue issue) {
            issues.add(issue);
        }

        public void removeIssue(IImportIssue issue) {
            issues.remove(issue);
        }

        public boolean isImportEnabled(Field field) {
            Boolean enabled = importFields.get(field);
            return enabled != null ? enabled.booleanValue() : true;
        }

        public void setImportEnabled(Field field, boolean enabled) {
            importFields.put(field, Boolean.valueOf(enabled));
        }

        public boolean isImportEnabled(ISongResource resource) {
            ResourceImport resImp = importResources.get(resource);
            return resImp != null ? resImp.enabled : true;
        }

        public void setImportEnabled(ISongResource resource, ISongResource dbResource, boolean overwrite) {
            importResources.put(resource, new ResourceImport(dbResource, overwrite));
        }

        public ISongResource getDbResource(ISongResource resource) {
            ResourceImport ri = importResources.get(resource);
            return ri != null ? ri.resource : null;
        }

        public void setImportEnabled(ISongResource resource, boolean enabled) {
            ResourceImport resImp = importResources.get(resource);
            if (resImp == null) {
                resImp = new ResourceImport(null, enabled);
                importResources.put(resource, resImp);
            } else {
                resImp.enabled = enabled;
            }
        }

    }

    public static class ImportIssue implements IImportIssue {

        private final ISongResource dbResource;
        private final ISongResource importResource;
        private final String description;
        private final Category category;
        private final Severity severity;

        private ImportIssue(Severity severity, ISongResource dbResource,
                ISongResource importResource, String description, Category category) {
            this.dbResource = dbResource;
            this.importResource = importResource;
            this.description = description;
            this.category = category;
            this.severity = severity;
        }

        public ImportIssue(Severity severity, String description, Category category) {
            this(severity, null, null, description, category);
        }

        public ImportIssue(Severity severity, ISongResource dbResource, ISongResource importResource, String description) {
            this(severity, dbResource, importResource, description, Category.RESOURCES);
        }

        public ISongResource getResourceInDb() {
            return dbResource;
        }

        public ISongResource getResourceToImport() {
            return importResource;
        }

        public String getDescription() {
            return description;
        }

        public Category getCategory() {
            return category;
        }

        public Severity getSeverity() {
            return severity;
        }

    }

    private static class ResourceImport {

        ISongResource resource;
        boolean enabled;

        public ResourceImport(ISongResource resource, boolean enabled) {
            this.resource = resource;
            this.enabled = enabled;
        }

    }

}
